package com.macro.mall.wx.service.impl;

import com.alibaba.fastjson.JSON;
import com.macro.mall.common.exception.ApiException;
import com.macro.mall.wx.API.WxAPI;
import com.macro.mall.wx.domain.WxRefundDTO;
import com.macro.mall.wx.sdk.WXPayConstants;
import com.macro.mall.wx.sdk.WXPayRequest;
import com.macro.mall.wx.sdk.WXPayUtil;
import com.macro.mall.wx.sdk.WxBeanConfig;
import com.macro.mall.wx.service.RefundService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
@Service
public class RefundServiceImpl implements RefundService {
    Logger logger = LoggerFactory.getLogger(RefundServiceImpl.class);
    @Autowired
    private WxBeanConfig wxBeanConfig;
    @Override
    public Map<String, String> refund(WxRefundDTO wxRefundDTO) {
        if(wxRefundDTO!=null){
            //1.封装参数
            Map<String, String> refundMap = JSON.parseObject(JSON.toJSONString(wxRefundDTO),Map.class);
            refundMap.put("appid",this.wxBeanConfig.getAppId());
            refundMap.put("mch_id",this.wxBeanConfig.getMchId());
            logger.info("申请退款refundMap:"+refundMap);
            logger.info("================ 分隔线 ==============");
            //2.生成签名,转换xml
            String refundSign =null;
            try {
                refundSign= WXPayUtil.generateSignedXml(refundMap, this.wxBeanConfig.getKey());
                logger.info("申请退款xml:"+refundSign);
                logger.info("================ 分隔线 ==============");
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(StringUtils.isNotBlank(refundSign)){
                //3.调用请求
                WXPayRequest request = WXPayRequest.getInstance(this.wxBeanConfig);
                try {
                    String result = request.doRefund(refundSign,this.wxBeanConfig.certPath);
                    logger.info("退款申请结果:"+result);
                    logger.info("================ 分隔线 ==============");
                    String RETURN_CODE = "return_code";
                    String return_code;
                    Map<String, String> respData = WXPayUtil.xmlToMap(result);
                    if (respData.containsKey(RETURN_CODE)) {
                        return_code = respData.get(RETURN_CODE);
                    }
                    else {
                        throw new ApiException(String.format("No `return_code` in XML: %s", result));
                    }

                    if (return_code.equals(WXPayConstants.FAIL)) {
                        String return_msg = respData.get("return_msg");
                        throw new ApiException(return_msg);
                    }
                    else if (return_code.equals(WXPayConstants.SUCCESS)) {
                        return respData;
                    }
                    else {
                        throw new ApiException(String.format("return_code value %s is invalid in XML: %s", return_code, result));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new ApiException("申请退款失败");
                }
            }
        }
        //5.处理返回结果
        return null;
    }
}
